<?php


namespace App\Models\StoreCenter\Models;


use App\Models\StoreCenter\StoreCenterModel;

class OrderModel extends StoreCenterModel
{
    const STATUS_WAITING = 10;
    const STATUS_PAYING = 20;
    const STATUS_PAYED = 30;
    const STATUS_sent = 40;
    const STATUS_ON_WAY = 50;
    const STATUS_SUCCESS = 60;
    const STATUS_CANCEL = 70;

    public $table = 'order';
    public $fillable = [
        'order_code', //varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '订单号',
        'create_user', //varchar(64) NOT NULL DEFAULT '' COMMENT '创建者',
        'create_user_uuid', //varchar(64) NOT NULL DEFAULT '' COMMENT '创建者uuid',
//        'create_time', //datetime DEFAULT NULL,
//        'update_time', //datetime DEFAULT NULL,
        'update_user', //varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '更新者',
        'update_user_uuid', //varchar(64) NOT NULL DEFAULT '' COMMENT '更新者uuid',
        'total_fee', //decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '总价（商品价+运费）',
        'actual_fee', //decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '实际支付（商品价+运费-折扣）',
        'reduce_fee', //decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '折扣',
        'shipping_fee', //decimal(10,2) DEFAULT '0.00' COMMENT '运费',
        'coupon', //varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '优惠券',
        'receiver', //varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '收货人',
        'email',
        'mobile', //varchar(64) NOT NULL DEFAULT '' COMMENT '收货人电话',
        'country', //varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '国家',
        'province', //varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '省',
        'city', //varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '市',
        'county', //varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '区',
        'address', //varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '详细地址',
        'status', //tinyint NOT NULL DEFAULT '10' COMMENT '状态 10=购物车 20=待支付 30=支付成功 40=已发货 50=在途 60=订单完成 70=订单取消 ',
    ];
    public function skuInfo()
    {
        return $this->hasMany(OrderSkuInfoModel::class, 'order_code', 'order_code');
    }

    public function getOrderInfo($where)
    {
        return $this->handleCondition($where)
            ->with([
                'skuInfo',
                'skuInfo.skuInfo:id,spu,sku,name,image_url,price,newest_price,stock',
                'skuInfo.spuInfo:id,spu,name,en_name,image_url',
            ])
            ->withCount('skuInfo')
            ->first();
    }

    public function getOrderList($data)
    {
        $where = [];
        is_real_exists(@$data['order_code']) && $where['order_code'] = ['like', '%'.$data['order_code'].'%'];
        is_real_exists(@$data['country']) && $where['country'] = ['like', '%'.$data['country'].'%'];
        is_real_exists(@$data['receiver']) && $where['receiver'] = ['like', '%'.$data['receiver'].'%'];
        is_real_exists(@$data['status']) && $where['status'] = $data['status'];
        is_real_exists(@$data['create_time']) && $where['create_time'] = ['between', $data['create_time']];

        $res = $this->handleCondition($where)
            ->with('skuInfo');

        if (is_real_exists(@$data['spu'])) {
            $res->whereHas('skuInfo', function ($query) use ($data) {
              $query->where('spu', $data['spu']);
            });
        }

        if (is_real_exists(@$data['sku'])) {
            $res->whereHas('skuInfo', function ($query) use ($data) {
                $query->where('sku', $data['sku']);
            });
        }

        return $res
            ->OrderBy('create_time', 'desc')
            ->paginate(config('pageSize'));

    }
}
